package com.drakulo.games.ais.ui.component;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.core.Resource;
import com.drakulo.games.ais.core.building.BuildingLevelData;
import com.drakulo.games.ais.ui.FontHelper;
import com.drakulo.games.ais.ui.I18n;

/**
 * This component display resource informations about a building in a table. It
 * can also make comparisons between two sets of resources informations.
 * 
 * @author Drakulo
 * 
 */
public class ResourceTable extends UIComponent {
	/** The basic data to display in the table */
	private BuildingLevelData basicData;
	/**
	 * If this map is filled, the contained values will be compared with the
	 * basicData values
	 */
	private BuildingLevelData compareData;

	private int columnWidth;

	public ResourceTable(int width) {
		this.width = width;
		this.columnWidth = width / 4;
	}

	@Override
	public void render(Graphics g) throws SlickException {
		// TODO Auto-generated method stub

		//
		Resource[] resources = Resource.values();
		final int fontSize = g.getFont().getLineHeight();
		final int resourceNumber = resources.length;
		final int tableHeight = resourceNumber * fontSize + fontSize;
		final int cellPadding = 2;
		Font font = FontHelper.getDefaultFont();

		// Draw the table
		// Vertical lines
		int dx = this.ox;
		int dy = this.oy;
		g.setColor(Color.black);
		g.drawLine(dx, dy, dx, dy + tableHeight);
		dx += this.columnWidth;
		g.drawLine(dx, dy, dx, dy + tableHeight);
		dx += this.columnWidth;
		g.drawLine(dx, dy, dx, dy + tableHeight);
		dx += this.columnWidth;
		g.drawLine(dx, dy, dx, dy + tableHeight);
		dx = this.ox + this.width; // Adjustment
		g.drawLine(dx, dy, dx, dy + tableHeight);

		// Horizontal lines
		dx = this.ox;
		dy = this.oy;
		g.drawLine(dx, dy, dx + this.width, dy);
		font.drawString(dx + cellPadding, dy, I18n.getFirstToUpper("resource"));

		dx += this.columnWidth;
		font.drawString(dx + cellPadding, dy, I18n.getFirstToUpper("resource.price"));

		dx += this.columnWidth;
		font.drawString(dx + cellPadding, dy, I18n.getFirstToUpper("resource.income"));

		dx += this.columnWidth;
		font.drawString(dx + cellPadding, dy, I18n.getFirstToUpper("resource.store"));

		dx = this.ox;
		dy += fontSize;
		String temp;
		for (Resource r : resources) {
			font.drawString(dx + cellPadding, dy, r.getI18n());
			g.drawLine(dx, dy, dx + this.width, dy);

			// Cost
			dx += this.columnWidth;
			temp = this.basicData.getUpgradeCost().get(r).toString();
			font.drawString(dx + cellPadding, dy, temp);

			// Production
			dx += this.columnWidth;
			temp = this.basicData.getProduction().get(r).toString();
			temp += " -> " + this.compareData.getProduction().get(r).toString();
			font.drawString(dx + cellPadding, dy, temp);

			// Store
			dx += this.columnWidth;
			temp = this.basicData.getStoreCapacity().get(r).toString();
			temp += "->" + this.compareData.getStoreCapacity().get(r).toString();
			font.drawString(dx + cellPadding, dy, temp);

			dy += fontSize;
			dx = this.ox;
		}
		g.drawLine(dx, dy, dx + this.width, dy);

	}

	@Override
	public void update(Input input) throws SlickException {
		// Nothing there
	}

	public void setBasicData(BuildingLevelData basicData) {
		this.basicData = basicData;
	}

	public void setCompareData(BuildingLevelData compareData) {
		this.compareData = compareData;
	}

	public void setWidth(int width) {
		this.width = width;
		this.columnWidth = width / 4;
	}

	public BuildingLevelData getBasicData() {
		return this.basicData;
	}

	public BuildingLevelData getCompareData() {
		return this.compareData;
	}

}
